#ifndef PICCOLO_VK_SWAPCHAIN_H
#define PICCOLO_VK_SWAPCHAIN_H

#include "PiccoloVKCommon.h"

namespace Piccolo
{
    class PiccoloVKGraphicsContext;
    class PiccoloVKDevice;

    struct SurfaceInfo
    {
        VkSurfaceCapabilitiesKHR capabilities;
        VkSurfaceFormatKHR surfaceFormat;
        VkPresentModeKHR presentMode;
    };

    class PiccoloVKSwapchain
    {
    public:
        PiccoloVKSwapchain(PiccoloVKGraphicsContext* context, PiccoloVKDevice* device);
        ~PiccoloVKSwapchain();

        bool RecreateSwapchain();

        VkResult AcquireImage(int32_t* outImageIndex, VkSemaphore semaphore, VkFence fence = VK_NULL_HANDLE);
        VkResult Present(int32_t imageIndex, const std::vector<VkSemaphore>& waitSemaphores);

        const std::vector<VkImage>& GetImages() const { return mSwapchainImages; }
        uint32_t GetWidth() const { return mSurfaceInfo.capabilities.currentExtent.width; }
        uint32_t GetHeight() const { return mSurfaceInfo.capabilities.currentExtent.height; }
        int32_t GetCurrentImageIndex() const { return mCurrentImageIndex; }

        const SurfaceInfo& GetSurfaceInfo() const { return mSurfaceInfo; }

    private:
        void SetupSurfaceCapabilities();

        VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;

        PiccoloVKGraphicsContext* mContext;
        PiccoloVKDevice* mDevice;

        std::vector<VkImage> mSwapchainImages;

        int32_t mCurrentImageIndex = -1;

        SurfaceInfo mSurfaceInfo;
    };
}

#endif